﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows;
using System.Windows.Controls;

namespace UIKit
{
    /// <summary>
    /// http://developer.apple.com/IPhone/library/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html
    /// </summary>
    public class UIViewController : UIResponder
    {
        #region properties
        

        /// <summary>
        /// control name OR type 4 part assembly name??? maybe using both of these to 
        /// create basic UserControl that has a control template?
        /// 
        /// </summary>
        public string nibName; 
        /// <summary>
        /// this could be a xapname to download and cache at a different level?
        /// maybe this class has a ref to the currentapplication and the app does the downloading of xaps.
        /// </summary>
        public NSBundle nibBundle;

        private UIView _view = null;
        public UIView view {
            get {
                if (_view == null) {
                    this.loadView();
                }
                return _view;
            }
            set {
                this._view = value;
            }
        }
        public String title;

        public bool wantsFullScreenLayout;
        public UIScreenOrientation interfaceOrientation;

        public UIViewController modalViewController;
        public UIViewController parentViewController;

        public UIModalTransitionStyle modalTransitionStyle;

        public UINavigationController navigationController;
        public UINavigationItem navigationItem;
        public bool editing;
        public bool hidesBottomBarWhenPushed;

        public UIBarButtonItem[] toolbarItems;

        public UITabBarController tabBarController;
        public UITabBarItem tabBarItem;
        public UISearchDisplayController searchDisplayController;

        #endregion

        #region methods
        
        /// <summary>
        /// look for a nib file whose name (without the .nib extension) matches the name of your view controller class. 
        /// If it finds one, the class name becomes the value of the nibName property, 
        /// which results in the corresponding nib file being associated with this view controller
        /// </summary>
        /// <param name="nibName"></param>
        /// <param name="nibBundle"></param>
        /// <returns></returns>
        public UIViewController initWithNibName(string nibName, NSBundle nibBundle)
        {
            // TODO: only do this if they are found!!
            NSBundle bdl = (nibBundle == null)? NSBundle.mainBundle(): nibBundle ;
            FileInfo fl = new FileInfo(nibName); // used to get rid of the extension
            object nibInstance = bdl.loadNibNamed(fl.Name, this, null);
            if (nibInstance!=null)
            {
                this.nibName = nibName;
                this.nibBundle = bdl;
            }
            return this;
        }
        
        /// <summary>
        /// Default implementation uses the nibname/bundle that was set initialized in initWithNibName.
        /// The default implementation of this method looks for valid nib information and uses that information to load the associated nib file. 
        /// If no nib information is specified, the default implementation creates a plain UIView object and makes it the main view.
        /// </summary>
        public virtual void loadView() {
            try
            {
                if (nibBundle == null || nibName == null)
                {
                    this._view = new UIView();
                }
                else { 
                    // load the nib, make sure it is of type "UIViewController"
                    // get it's properties?
                    // get it's view
                    object[] rawObjects = this.nibBundle.loadNibNamed(nibName, this, null);
                    
                    // LG: as we are not implementing the full nib/xib structure the first
                    // control will be an instance of the type we asked for.
                    if (rawObjects.Length>0){
                        object rawObject = rawObjects[0];

                        if (rawObject is UIView) {
                            this.view = rawObject as UIView;
                        }
                        else if (rawObject is FrameworkElement)
                        {
                            UIView newview = new UIView();
                            newview.viewElement = rawObject as FrameworkElement;
                            this._view = newview;
                        }
                    }
                }
                this.viewDidLoad();
            }
            catch { 
                // arrghhh!!!
            }
        }

        public virtual void viewDidLoad() {}
        public virtual void viewDidUnload() {}
        public bool isViewLoaded()
        {
            // TODO: implement
            return true;
        }

        //shouldAutorotateToInterfaceOrientation
        //rotatingHeaderView
        //rotatingFooterView

        //didReceiveMemoryWarning

        public void presentModalViewController(UIViewController modalViewController, bool animated) { }
        public void dismissModalViewControllerAnimated(bool animated) { }

        public void setEditing(bool editing, bool animated) { }
        public UIBarButtonItem editButtonItem()
        {
            // TODO: implement
            return new UIBarButtonItem();
        }

        public void setToolbarItems(bool animated) { }

        #endregion


        #region event notifications
        //# – viewWillAppear:
        //# – viewDidAppear:
        //# – viewWillDisappear:
        //# – viewDidDisappear:
        //# – willRotateToInterfaceOrientation:duration:
        //# – willAnimateRotationToInterfaceOrientation:duration:
        //# – didRotateFromInterfaceOrientation:
        //# – willAnimateFirstHalfOfRotationToInterfaceOrientation:duration:
        //# – didAnimateFirstHalfOfRotationToInterfaceOrientation:
        //# – willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:
        #endregion

    }
}
